//
//  AppController.h
//  CocoaFan
//
//  Created by Chen Xianan on 2009-02-16.
//  Copyright 2009 realazy.org All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import "MGTwitterEngine.h"
#import <Growl/GrowlApplicationBridge.h>

#define COCOAFAN_CELL_AVATARWIDTH 48.0
#define COCOAFAN_CELL_AVATARHEIGHT 48.0
#define COCOAFAN_CELL_OCCUPIEDWIDTH 117.0
#define COCOAFAN_CELL_OCCUPIEDHEIGHT 41.0

extern NSString * const COCOAFAN_KEY_USERNAME;
extern NSString * const COCOAFAN_KEY_PASSWORD;
extern NSString * const COCOAFAN_KEY_AUTOLOGIN;
extern NSString * const COCOAFAN_KEY_STARTATLOGIN;
extern NSString * const COCOAFAN_KEY_UPDATE_INTERVAL;

typedef enum {
	COCOAFAN_STATUS_NORMAL,
	COCOAFAN_STATUS_OWN,
	COCOAFAN_STATUS_REPLY,
	COCOAFAN_STATUS_PMSG
} COCOAFAN_STATUS_TYPE;

@class COCOAFANCell, COCOAFANTableView, COCOAFANMessageBoardView, COCOAFANPmsgWindowController;
@class IFVerticallyExpandingTextField;

@interface AppController : NSObject <MGTwitterEngineDelegate, GrowlApplicationBridgeDelegate> {
	// main window
	IBOutlet NSWindow *window;
	IBOutlet COCOAFANTableView *table;
	IBOutlet IFVerticallyExpandingTextField *textInput;
	IBOutlet NSTextField *counter;
	IBOutlet COCOAFANMessageBoardView *msgBoard;
	IBOutlet NSSearchField *searchField;
	
	// login sheet
	IBOutlet NSWindow *loginSheet;
	IBOutlet NSTextField *username;
	IBOutlet NSSecureTextField *password;
	IBOutlet NSTextField *msgField;
	
	MGTwitterEngine *engine;

	BOOL isLogined;
	// request identifier
	NSString *loginUUID;
	NSString *sendUUID;
	NSString *frdTimelineUUID;
	NSString *userInfoUUID;
	NSString *repliesUUID;
	NSString *directUUID;
	NSString *deleteUUID;
	NSString *sendPmsgUUID;
		
	NSMutableArray *statusesArray;
	NSArray *statusesArrayClone;
	NSMutableArray *tmpStatuses;
	NSMutableDictionary *cacheCells;
	NSDictionary *userInfo;
	NSDictionary *selectedStatusContent;
	NSTimer *timer;

	COCOAFANPmsgWindowController *pmsgController;
}

@property (retain) NSDictionary *userInfo;

+ (NSString *)readableDate:(NSDate *)date;
+ (void)openMessageInBrowser:(NSDictionary *)dic;
+ (void)openProfileInBrowser:(NSDictionary *)dic;
- (void)showSheet:(NSWindow *)sheetWindow;
- (void)hideSheet:(NSWindow *)sheetWindow;
- (void)loadStatuses;
- (void)sendStatus;
- (void)handleReceiveItems:(NSArray *)items;
- (NSArray *)uniqArray:(NSArray *)array;
- (COCOAFAN_STATUS_TYPE)statusType:(NSDictionary *)statusItem;
- (BOOL)allRequestsReturned;
- (void)handleIdentifier:(NSString *)identifier andItems:(NSArray *)items;
- (NSDictionary *)findStatusByID:(NSString *)status_id;
- (COCOAFANCell *)cellByID:(NSString *)status_id;
- (NSInteger)findRowByID:(NSString *)status_id;
- (NSString *)findCurrentStatusID;
- (void)selectedRowAndScrollToVisible;
- (void)filterByString:(NSString *)str;
- (void)updateReadableDate;
- (void)resetTimer;
- (void)focusTextField:(NSTextField *)textField;
- (void)handleReply:(NSNotification *)note;
- (void)handlePmsg:(NSNotification *)note;
- (void)handleSendPmsg:(NSNotification *)note;
- (void)handleForward:(NSNotification *)note;
- (void)handleDelete:(NSNotification *)note;
- (NSDictionary *)transformStatus:(NSDictionary *)statusDic;
- (NSAttributedString *)_attributedText:(NSDictionary *)statusDic;
- (NSAttributedString *)_attributedAdditionalInformation:(NSDictionary *)statusDic;

- (IBAction)login:(id)sender;
- (IBAction)terminateApp:(id)sender;
- (IBAction)reply:(id)sender;
- (IBAction)pmsg:(id)sender;
- (IBAction)forward:(id)sender;
- (IBAction)delete:(id)sender;
- (IBAction)openMessageInBrowser:(id)sender;
- (IBAction)openProfileInBrowser:(id)sender;
- (IBAction)refresh:(id)sender;
- (IBAction)changeLogin:(id)sender;

- (IBAction)updateFilter:(id)sender;

@end
